from .single import SingletonMeta
import redis.asyncio as redis
import settings

class TLLRedis(metaclass=SingletonMeta):
    SMS_CODE_PREFIX="sms_code_{}"
    REFRESH_TOKEN_PREFIX="refresh_token_{}"
    def __init__(self):
        self.client=redis.Redis(
            host=settings.REDIS_HOST,
            port=int(settings.REDIS_PORT),
            db=int(settings.REDIS_DB),
            password=settings.REDIS_PASSWORD,
            encoding="utf-8",
            decode_responses=True,
            socket_timeout=float(settings.REDIS_TIMEOUT),
            socket_connect_timeout=float(settings.REDIS_TIMEOUT),
        )
        
    async def set(self,key:str,value:str,expire:int=5*60*60):
        await self.client.set(key,value,ex=expire)
        
    async def get(self,key:str):
        value=await self.client.get(key)
        if type(value)==bytes:
            value=value.decode('utf-8')
        return value
    
    async def delete(self,key:str):
        await self.client.delete(key)
        
    async def set_sms_code(self, mobile, code):
        await self.set(self.SMS_CODE_PREFIX.format(mobile), code)

    async def get_sms_code(self, mobile):
        code = await self.get(self.SMS_CODE_PREFIX.format(mobile))
        return code

    async def set_refresh_token(self, user_id, refresh_token):
        ex = settings.JWT_REFRESH_TOKEN_EXPIRES
        await self.set(self.REFRESH_TOKEN_PREFIX.format(user_id), refresh_token, expire=int(settings.JWT_REFRESH_TOKEN_EXPIRES.total_seconds()))

    async def get_refresh_token(self, user_id):
        refresh_token = await self.get(self.REFRESH_TOKEN_PREFIX.format(user_id))
        return refresh_token

    async def delete_refresh_token(self, user_id):
        await self.delete(self.REFRESH_TOKEN_PREFIX.format(user_id))

    async def close(self):
        await self.client.aclose()